package com.栈;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 设计栈
 */
public class MinStack {
    List<Integer> data ;

    int min; //最小值
    public MinStack() {
        data = new ArrayList<>();
        min =  -1;
    }

    public void push(int x) {

        min = x;
        data.add(x);
    }

    public void pop() {
        if(data.size()==0){
            return;
        }
        data.remove(data.size()-1);
        if(data.size()==0){
            return;
        }
        min = data.get(data.size()-1);
    }

    public int top() {
            return  data.get(data.size()-1);
    }

    public int getMin() {
       if(data.size()==0){
           return -1;
       }
       for(int i : data){
           if(i<min){
               min = i;
           }
       }
      return min;
    }

    @Override
    public String toString() {
      StringBuilder builder = new StringBuilder();
      for(int i : data){
          builder.append(i+"   ");
      }
      return String.format("Stack{%s}",builder.toString());
    }

    public static void main(String[] args) {
        MinStack stack = new MinStack();

        int  i;
        while (true){
            // System.out.println("出队输入0");
            //System.out.println("结束输入-1");
            //-2  查询栈头
            //-3 最小栈
            System.out.println("输入入栈数字：");
            i = new Scanner(System.in).nextInt();
            if(i == -2){

                    System.out.println("栈头:"+stack.top());

                    continue;

                }
            if((i == -3)){
                System.out.println("最小栈:"+stack.getMin());
                continue;
            }


            if(i == -1){
                System.out.println("结束栈");
                break;
            }
            if(i==0){

                stack.pop();
                System.out.println(stack.toString());
                continue;
            }
                //入栈
                stack.push(i);
                System.out.println(stack.toString());



        }
    }
}
